<template>
  <div>
    <!-- 列表页面 -->
    <div class="container" v-if="!showEdit">
      <div class="header">
        <div class="title">用户列表</div>
        <div>
          <el-button @click="exprotExcel">导出</el-button>
        </div>
      </div>
      <!-- 表格 -->
      <lin-table
        :tableColumn="tableColumn"
        :tableData="tableData"
        :operate="operate"
        @handleEdit="handleEdit"
        @handleView="handleView"
        @handleDelete="handleDelete"
        @row-click="rowClick"
        v-loading="loading"
      ></lin-table>
      <div class="pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        :background="true"
        :page-size="pageCount"
        :current-page="currentPage"
        v-if="refreshPagination"
        layout="prev, pager, next, jumper"
        :total="totalNums"
      >
      </el-pagination>
    </div>
    </div>
    <el-dialog title="用户信息" :append-to-body="true" :before-close="handleClose" :visible.sync="dialogFormVisible">
      <div style="margin-top: -25px">
        <el-tabs v-model="activeTab" @tab-click="handleClick">
          <el-tab-pane label="修改信息" name="修改信息">
            <oms-edit v-if="dialogFormVisible" @editClose="editClose" pageType="edit" :info="form" :editWmsUserID="editWmsUserID"></oms-edit>
          </el-tab-pane>
          <el-tab-pane label="修改密码" name="修改密码">
            <oms-password @editClose="editClose" :editWmsUserID="editWmsUserID"></oms-password>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-dialog>
    <el-dialog title="" :append-to-body="true" :before-close="handleClose" :visible.sync="dialogFormView">
      <div style="margin-top: -25px">
        <oms-view v-if="dialogFormView" @editClose="editClose" :viewWmsUserID="viewWmsUserID"></oms-view>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import omsuser from '@/model/omsuser'
import LinTable from '@/component/base/table/lin-table'
// import BookModify from './book-modify'
import ParseTime from '@/lin/util/parse-time'
import OmsEdit from './oms-edit.vue'
import OmsPassword from './oms-password.vue'
import OmsView from './oms-view.vue'

export default {
  components: {
    LinTable,
    OmsEdit,
    OmsPassword,
    OmsView,
    // BookModify,
  },
  data() {
    return {
      refreshPagination: true, // 页数增加的时候，因为缓存的缘故，需要刷新Pagination组件
      totalNums: 0,
      currentPage: 1,
      pageCount: 10,
      dialogFormVisible: false,
      dialogFormView: false,
      activeTab: '修改信息',
      tableColumn: [
        { prop: 'username', label: '用户名' },
        { prop: 'email', label: '邮箱' },
      ],
      tableData: [],
      operate: [],
      showEdit: false,
      editWmsUserID: 1,
      viewWmsUserID: 1,
      form: {
        // 表单信息
        username: '',
        email: ''
      },
    }
  },
  async created() {
    this.loading = true
    await this.getWmsUser()
    this.operate = [
      { name: '查看', func: 'handleView', type: 'primary' },
      { name: '编辑', func: 'handleEdit', type: 'primary' },
      {
        name: '删除',
        func: 'handleDelete',
        type: 'danger',
        permission: '删除用户',
      },
    ]
    this.loading = false
  },
  methods: {
    async getWmsUser() {
      try {
        const page = this.currentPage - 1
        const count = this.pageCount
        const warehouseList = await omsuser.getWmsUserList(page, count)
        this.tableData = warehouseList.items
        this.totalNums = warehouseList.total
      } catch (error) {
        if (error.code === 10020) {
          this.tableData = []
        }
      }
    },
    // 切换table页
    async handleCurrentChange(val) {
      console.log(val);
      this.currentPage = val
      this.loading = true
      this.getWmsUser()
      this.loading = false
    },
    handleEdit(val) {
      console.log(val);
      this.form.username = val.row.username;
      this.form.email = val.row.email;
      console.log(this.form);
      console.log('val', val.row.id)
      // this.showEdit = true
      this.editWmsUserID = val.row.id
      this.dialogFormVisible = true
      console.log(val.row.id)
    },
    handleView(val) {
      console.log('val', val.row.id)
      this.dialogFormView = true
      this.viewWmsUserID = val.row.id
    },
    handleDelete(val) {
      console.log(val)
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await omsuser.deleteWmsUser(val.row.id)
        this.getWmsUser()
        if (res.code < window.MAX_SUCCESS_CODE) {
          
          this.$message({
            type: 'success',
            message: `${res.message}`,
          })
        }
      })
    },
    handleClose(done) {
      this.dialogFormVisible = false
      console.log(done);
      done()
    },
    rowClick() {},
    editClose() {
      console.log('关闭弹窗');
      this.dialogFormView = false
      this.getWmsUser()
    },
    // 切换tab栏
    handleClick(tab) {
      this.activeTab = tab.name
    },
    // 导出表格
    exprotExcel() {
      // 动态导入
      import('@/lin/util/export-excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'label', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'label', 'importance', 'status']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list',
        })
      })
    },
    // 将表单格式化为json数据
    formatJson(filterVal) {
      return this.tableData.map(v =>
        filterVal.map(j => {
          if (j === 'timestamp') {
            return ParseTime(v[j])
          }
          return v[j]
        }),
      )
    },
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
